package com.liujit.bms.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.admin.service.BlogMemberService;
import com.liujit.bms.admin.domain.model.BlogMember;
import com.liujit.bms.admin.domain.query.BlogMemberQuery;
import com.liujit.bms.admin.domain.result.BlogMemberResult;
import com.liujit.bms.admin.mapper.BlogMemberMapper;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import org.springframework.stereotype.Service;

/**
 * @Description 博客会员Service实现类
 * @Author liujun
 * @Date 2021-07-02 14:27:58
 */
@Service
public class BlogMemberServiceImpl extends ServiceImpl<BlogMemberMapper, BlogMember> implements BlogMemberService {

    @Override
    public PageBean<BlogMemberResult> getPage(BlogMemberQuery query) {
        // 排序
        if (StrUtil.isNotBlank(query.getSortName()) && StrUtil.isNotBlank(query.getSortOrder())) {
            query.setSortName(ColumnConstant.CREATE_TIME);
            query.setSortOrder(SqlKeyword.DESC.getSqlSegment());
        }
        Page<BlogMember> page = Pagination.page(query);
        LambdaQueryWrapper<BlogMember> wrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(query.getEmail())) {
            wrapper.like(BlogMember::getEmail, query.getEmail());
        }
        if (StrUtil.isNotBlank(query.getMobile())) {
            wrapper.like(BlogMember::getMobile, query.getMobile());
        }
        if (StrUtil.isNotBlank(query.getNickName())) {
            wrapper.like(BlogMember::getNickName, query.getNickName());
        }
        if (query.getEnabled() != null) {
            wrapper.eq(BlogMember::getEnabled, query.getEnabled());
        }
        if (query.getStartTime() != null) {
            wrapper.ge(BlogMember::getCreateTime, query.getStartTime());
        }
        if (query.getEndTime() != null) {
            wrapper.le(BlogMember::getCreateTime, query.getEndTime());
        }
        IPage<BlogMember> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, BlogMemberResult.class);
    }

    @Override
    public BlogMemberResult getDetail(Long id) {
        BlogMember blogMember = super.getById(id);
        if (blogMember == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(blogMember, BlogMemberResult.class);
    }

    @Override
    public Boolean enabled(BaseParams params) {
        BlogMember blogMember = super.getById(params.getId());
        if (blogMember == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new BlogMember() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_YES);
        }});
    }

    @Override
    public Boolean disabled(BaseParams params) {
        BlogMember blogMember = super.getById(params.getId());
        if (blogMember == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new BlogMember() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_NO);
        }});
    }
}
